package com.mvvmhabit.org.base

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatDialog
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import com.mvvmhabit.org.bus.Messenger

abstract class BaseDialog<V : ViewDataBinding, VM : BaseViewModel>(val context: FragmentActivity) :

    AppCompatDialog(context) {
    lateinit var mBinding: V

    var mViewModel: VM? = null

    private var viewModelId: Int = 0

    abstract fun layoutId(savedInstanceState: Bundle?): Int

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
    abstract fun initVariableId(): Int

    /**
     * 初始化ViewModel
     *
     * @return 继承BaseViewModel的ViewModel
     */
    abstract fun initViewModel(): Class<VM>

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
//    abstract fun initVariableId(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            layoutId(savedInstanceState),
            null,
            false
        )
        setContentView(mBinding.root)
        if (mViewModel == null) {
            println("onCreate 中的ViewModel")
            mViewModel = ViewModelProvider(context).get(initViewModel())
            viewModelId = initVariableId()
            mBinding.setVariable(viewModelId, mViewModel)
            context.lifecycle.addObserver(mViewModel!!)
            mViewModel?.injectLifecycleProvider(context)
            registerUIChangeLiveEventCallBack()
            initData()
            initViewObservable()
        }
        mViewModel?.registerBus()
    }

    private fun registerUIChangeLiveEventCallBack() {
        // 页面跳转
        mViewModel?.getUI()?.getStartActivityEvent()?.observeForever {
            val clz: Class<*> = it[CLASS] as Class<*>
            val bundle: Any? = it[BUNDLE]
            startActivity(clz, bundle)
        }
        // 跳转到容器页面
        mViewModel?.getUI()?.getStartContainerActivityEvent()?.observeForever {
            val canonicalName = it[CANONICAL_NAME] as String
            val bundle = it[BUNDLE]
            startContainerActivity(canonicalName, bundle)
        }
        // 关闭界面
        mViewModel?.getUI()?.getFinishEvent()?.observeForever {
            context.finish()
        }

        mViewModel?.getUI()?.getStartIntentEvent()?.observeForever {
            context.startActivity(it)
        }
    }

    protected fun startActivity(clz: Class<*>, bundle: Any?) {
        val intent = Intent(context, clz)
        if (bundle != null) {
            intent.putExtras(bundle as Bundle)
        }
        context.startActivity(intent)
    }

    protected fun startContainerActivity(canonicalName: String?, bundle: Any?) {
        val intent = Intent(context, ContainerActivity::class.java)
        intent.putExtra(FRAGMENT, canonicalName)
        if (bundle != null) {
            intent.putExtra(FRAGMENT_BUNDLE, bundle as Bundle)
        }
        context.startActivity(intent)
    }


    open fun initData() {

    }

    open fun initViewObservable() {

    }

    open fun onDestroy() {
        Messenger.getDefault().unregister(mViewModel)
        if (mViewModel != null) {
            mViewModel?.removeBus()
        }
        mBinding.unbind()
    }
}